package com.changhai.diabetes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.changhai.diabetes.model.PhysicalSurvey;
import com.changhai.diabetes.model.PhysicalSurveyResult;
import com.changhai.diabetes.model.QualitySurvey;
import com.changhai.diabetes.model.QualitySurveyResult;
import com.changhai.diabetes.model.SymptomsSurvey;
import com.changhai.diabetes.model.SymptomsSurveyResult;
import com.changhai.diabetes.service.SurveyService;
import com.changhai.diabetes.util.Constants;

/**
 * 问卷调查表（生存质量特性、症候、体质分类）
 * 
 * @author Andy
 * 
 */
public class SurveyServiceImpl extends CommonServiceImpl implements SurveyService {

	public List<QualitySurvey> searchQualitySurveyQuestions() {
		List<QualitySurvey> list = new ArrayList<QualitySurvey>();
		list = this.getList(Constants.DS_CHANGHAI, "qualitySurvey.getAllSurveys", null);
		return list;
	}

	public QualitySurveyResult searchQualityAnswerByPatientId(Long patientId) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "qualitySurveyResult.getByPatientId", patientId);
		if (o != null && o instanceof QualitySurveyResult) {
			return (QualitySurveyResult) o;
		} else {
			QualitySurveyResult s = new QualitySurveyResult();
			s.setPatientid(patientId);
			return s;
		}
	}

	public void saveEditQualityAnswer(QualitySurveyResult qualityResult) {
		if (qualityResult != null) {
			if (qualityResult.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "qualitySurveyResult.updateByPrimaryKey", qualityResult);
			} else {
				Long id = this.saveNewQualityAnswer(qualityResult);
				if (id != null && id > 0) {
					qualityResult.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "qualitySurveyResult.updateByPrimaryKey", qualityResult);
				}
			}
		}
	}

	public Long saveNewQualityAnswer(QualitySurveyResult qualityResult) {
		Integer id = 0;
		if (qualityResult != null && qualityResult.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "qualitySurveyResult.insert", qualityResult);
		}
		return new Long(id);
	}

	public List<SymptomsSurvey> searchSymptomsSurveyQuestions() {
		List<SymptomsSurvey> list = new ArrayList<SymptomsSurvey>();
		list = this.getList(Constants.DS_CHANGHAI, "symptomsSurvey.getAllSurveys", null);
		return list;
	}

	public SymptomsSurveyResult searchSymptomsAnswerByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "symptomsSurveyResult.getByPatientId", patientid);
		if (o != null && o instanceof SymptomsSurveyResult) {
			return (SymptomsSurveyResult) o;
		} else {
			SymptomsSurveyResult s = new SymptomsSurveyResult();
			s.setPatientid(patientid);
			return s;
		}
	}

	public void saveEditSymptomsAnswer(SymptomsSurveyResult symptomsResult) {
		if (symptomsResult != null) {
			if (symptomsResult.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "symptomsSurveyResult.updateByPrimaryKey", symptomsResult);
			} else {
				Long id = this.saveNewSymptomsAnswer(symptomsResult);
				if (id != null && id > 0) {
					symptomsResult.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "symptomsSurveyResult.updateByPrimaryKey", symptomsResult);
				}
			}
		}
	}

	public Long saveNewSymptomsAnswer(SymptomsSurveyResult symptomsResult) {
		Integer id = 0;
		if (symptomsResult != null && symptomsResult.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "symptomsSurveyResult.insert", symptomsResult);
		}
		return new Long(id);
	}

	public List<PhysicalSurvey> searchPhysicalSurveyQuestions() {
		List<PhysicalSurvey> list = new ArrayList<PhysicalSurvey>();
		list = this.getList(Constants.DS_CHANGHAI, "physicalSurvey.getAllSurveys", null);
		return list;
	}

	public PhysicalSurveyResult searchPhysicalAnswerByPatientId(Long patientId) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "physicalSurveyResult.getByPatientId", patientId);
		if (o != null && o instanceof PhysicalSurveyResult) {
			return (PhysicalSurveyResult) o;
		} else {
			PhysicalSurveyResult s = new PhysicalSurveyResult();
			s.setPatientid(patientId);
			return s;
		}
	}

	public void saveEditPhysicalAnswer(PhysicalSurveyResult physicalResult) {
		if (physicalResult != null) {
			if (physicalResult.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "physicalSurveyResult.updateByPrimaryKey", physicalResult);
			} else {
				Long id = this.saveNewPhysicalAnswer(physicalResult);
				if (id != null && id > 0) {
					physicalResult.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "physicalSurveyResult.updateByPrimaryKey", physicalResult);
				}
			}
		}
	}

	public Long saveNewPhysicalAnswer(PhysicalSurveyResult physicalResult) {
		Integer id = 0;
		if (physicalResult != null && physicalResult.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "physicalSurveyResult.insert", physicalResult);
		}
		return new Long(id);
	}

	public Integer getGenderForQuestion(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "patient.getGenderByPatientId", patientid);
		if (o != null && o instanceof Integer) {
			return (Integer) o;
		} else {
			return null;
		}
	}
}
